﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zero.Core;
using Zero.Web.ViewModels;
using Zero.Core.Extensions;
using Zero.Entity.Models;
using Zero.Service;

namespace Zero.Web.Controllers
{
    public class MenusController : BaseController
    {
        private readonly IMenuService menuService;
        public MenusController(IMenuService menuService)
        {
            this.menuService = menuService;
        }

        public ActionResult Index()
        {
            string strUserID = SysContext.CurrentUserID;
            Sys_Menu Menus = menuService.GetAll().Where(m => m.MenuParentID == null).FirstOrDefault();
            return View(Menus);
        }

        public ActionResult MenuList(string ID)
        {
            Sys_Menu menu = menuService.Get(ID);
            return View(menu);
        }


        //Update a existed Menu
        public ActionResult MenuDetail(string ID, string Op)
        {
            DetailViewModel<Sys_Menu> vm = new DetailViewModel<Sys_Menu>();
            Sys_Menu menu = new Sys_Menu();
            
           
            switch (Op.ToUpper())
            {
                case "ADD":
                    {
                        vm.IsAdd = true;
                        vm.OpMethod ="Add";
                        menu.MenuID = Guid.NewGuid().ToStr();
                        menu.MenuNo =( menuService.Get(ID).ChildMenus.Max(m=>m.MenuNo).ToInt()+1).ToStr();
                        menu.IsEnable = true;
                        menu.MenuParentID = ID;
                        break;
                    }
                case "EDIT":
                    {
                        vm.IsEdit = true;
                        vm.OpMethod = "Update";
                        menu = menuService.Get(ID);
                        break;
                    }
                case "VIEW":
                    {
                        vm.IsView = true;
                        menu = menuService.Get(ID);
                        break;
                    }
                default:
                    break;
            }
            vm.Model = menu;
            return View(vm);
        }

        [HttpPost,ActionName("Add")]
        public ActionResult Create(FormCollection form)
        {
            Sys_Menu m = new Sys_Menu();
            m.Load(form);
            
            
            menuService.Add(m);
            StatusJsonHelper sjh = new StatusJsonHelper();

            sjh.message = "添加按钮成功";
            sjh.statusCode = "200";
            sjh.callbackType = "closeCurrent";
            sjh.navTabId = "menu0902";
            sjh.rel = "jbsxBox";
            return Json(sjh);
        }
    }
}
